package com.bayair.doctor.utils;

import java.util.ArrayList;
import java.util.List;

/**
  防止过快
 */
public class NoQuickClick {

    // 两次点击按钮之间的点击间隔不能少于1500毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1500;

    private static long lastClickTime;

    public static boolean isQuickClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    //保存click时间
    static List<Long> list = new ArrayList<>();
    //最短间隔
    private static final int MIN_CLICK_INTERVAL_TIME = 3000;

    public static boolean isMultiClick() {
        long curTime = System.currentTimeMillis();
        if (list.isEmpty()) {
            list.add(curTime);
        } else {
            if (isValid(curTime)) {
                list.add(curTime);
                if (list.size() >= 3) {
                    return true;
                }
            } else {
                List<Long> removeList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    Long l = list.get(i);
                    if (curTime - l > MIN_CLICK_INTERVAL_TIME) {
                        removeList.add(l);
                    }
                }
                list.removeAll(removeList);
                list.add(curTime);
            }
        }
        return false;
    }

    //此次点击是否在限制时间内
    private static boolean isValid(long cur) {
        for (Long time : list) {
            if (cur - time > MIN_CLICK_INTERVAL_TIME) {
                return false;
            }
        }
        return true;
    }
}
